﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Plateformeur.GameEntities
{
	public class Player : Engine.ControllerGameObject
	{
		private int mScore;
		private bool mInvicible;
		private float mBlinkTime;
        // BULLETS
        protected List<Bullet> mFiredBullet;
        private float mLastShoot;

        public Player()
            :base(Vector2.Zero, 20.0f, "SpriteSheet_Player", new Vector2(40, 100))
        {
            mInput = new Engine.PlayerController();
            mRenderedRect = new Rectangle(0, 0, 44, 44);
            AddAnimation("idle", "SpriteSheet_Player", 1.0f, 1);
            AddAnimation("walk", "SpriteSheet_Player", 1.0f, 4);
            Play("idle");
            mLastShoot = 0.0f;
            mFiredBullet = new List<Bullet>();
        }


        public void Walk()
        {
            Play("walk");
        }

        public void Idle()
        {
            Play("idle");
        }
        public void Shoot()
        {

            if (mLastShoot == 0)
            {
                Bullet bullet = new Bullet(1.0f, new Vector2(1.0f, 0.0f), 30.0f, Position + new Vector2(base.RenderRectangle.Width + 16, base.RenderRectangle.Height / 2));
                Plateformeur.Engine.GameObject.sGame.Components.Add(bullet);
                mFiredBullet.Add(bullet);
                mLastShoot = 1.0f;
            }
        }

		public override void Update(GameTime pTime)
		{
			List<Ennemy> toRemoveList = new List<Ennemy>();
            mLastShoot -= (float)pTime.ElapsedGameTime.TotalSeconds;

            if (mLastShoot < 0)
                mLastShoot = 0;

            List<Bullet> mToDelete = new List<Bullet>();

            foreach (Bullet lBullet in mFiredBullet)
            {
                Ennemy lTarg = null;
                foreach (Ennemy lTarget in Ennemy.sList)
                {
                    Engine.IntersectionInfo lInfo = lBullet.Intersect(lTarget);
                    if (lInfo.mCollideInfo.mCollide)
                    {
                        lTarg = lTarget;
                        lTarget.Destroy();
                        mToDelete.Add(lBullet);
                        break;
                    }
                }

                if (lTarg != null)
                {
                    Ennemy.sList.Remove(lTarg);
                    break;
                }

                if (lBullet.Position.Y < -30)
                {
                    mToDelete.Add(lBullet);
                }
            }

            foreach (Bullet lBullet in mToDelete)
            {
                mFiredBullet.Remove(lBullet);
                Game.Components.Remove(lBullet);
            }

            foreach (Ennemy ennemy in Ennemy.sList)
            {
                Engine.IntersectionInfo intersect = this.Intersect(ennemy);
                if (intersect.mCollideInfo.mCollide)
                {
                    if (mVelocity.Y > 1.0f)
                    {
                        toRemoveList.Add(ennemy);
                        mVelocity.Y *= -0.5f;
                        Game1.score += 1;
                    }
                    else
                    {
                        Game1.sGameOver = true;
                        Game.Components.Remove(this);
                    }
                }
            }


            foreach (Ennemy ennemy in toRemoveList)
            {
                Ennemy.sList.Remove(ennemy);
                Game.Components.Remove(ennemy);
            }
            

            base.Update(pTime);
		}
	}
}
